package com.ruoyi.web.controller.property.garbage;

/**
 * @Author: ruohao
 * @Date: 2023/9/2
 **/

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.GarbageRecord;
import com.ruoyi.common.core.domain.entity.GarbageRecordDetail;
import com.ruoyi.common.core.domain.entity.GarbageType;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.GarbageRecordDetailService;
import com.ruoyi.system.service.GarbageRecordService;
import com.ruoyi.system.service.GarbageTypeService;
import com.ruoyi.system.service.resp.GarbageRecordDetailDTO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/property/garbage/record/detail")
public class RecordDetailController extends BaseController {
    @Resource
    private GarbageRecordDetailService garbageRecordDetailService;

    @Resource
    private GarbageRecordService garbageRecordService;

    @Resource
    private GarbageTypeService garbageTypeService;

    @GetMapping("/list")
    public TableDataInfo list(GarbageRecordDetail garbageRecordDetail) {
        startPage();
        List<GarbageRecordDetail> list = garbageRecordDetailService.selectList(garbageRecordDetail);
        return getDataTable(fillToDTO(list));
    }

    @GetMapping("/listAll")
    public TableDataInfo listAll(GarbageRecordDetail garbageRecordDetail) {
        List<GarbageRecordDetail> list = garbageRecordDetailService.selectAll(garbageRecordDetail);
        return getDataTable(fillToDTO(list));
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(fillOneToDTO(garbageRecordDetailService.selectById(id)));
    }

    @Log(title = "垃圾记录管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody GarbageRecordDetail garbageRecordDetail) {
        garbageRecordDetail.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = garbageRecordDetailService.create(garbageRecordDetail);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "垃圾记录管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult edit(@Validated @RequestBody GarbageRecordDetail garbageRecordDetail) {
        garbageRecordDetail.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = garbageRecordDetailService.update(garbageRecordDetail);

        GarbageRecordDetail garbageRecordDetail1 = garbageRecordDetailService.selectById(garbageRecordDetail.getId());

        GarbageRecord search = garbageRecordService.selectGarbageRecordById(garbageRecordDetail1.getGarbageRecordId());
        if (search == null || !search.getCreateBy().equals(SecurityUtils.getLoginUser().getUser().getNickName())) {
            throw new BaseException("该单据由" + search.getCreateBy() + "创建，您无法修改");
        }

        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "垃圾记录管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@PathVariable Long id) {
        GarbageRecordDetail garbageRecordDetail1 = garbageRecordDetailService.selectById(id);

        GarbageRecord search = garbageRecordService.selectGarbageRecordById(garbageRecordDetail1.getGarbageRecordId());
        if (search == null || !search.getCreateBy().equals(SecurityUtils.getLoginUser().getUser().getNickName())) {
            throw new BaseException("该单据由" + search.getCreateBy() + "创建，您无法修改");
        }
        return toAjax(garbageRecordDetailService.deleteById(id));
    }

    private List<GarbageRecordDetailDTO> fillToDTO(List<GarbageRecordDetail> garbageRecordDetails) {
        if (CollectionUtils.isEmpty(garbageRecordDetails)) {
            return new ArrayList<>();
        }
        List<Long> garbageTypeIds = garbageRecordDetails.stream().map(GarbageRecordDetail::getGarbageTypeId).collect(Collectors.toList());
        Map<Long, GarbageType> garbageTypeMap = garbageTypeService.selectByIds(garbageTypeIds).stream().collect(Collectors.toMap(GarbageType::getId, g -> g));
        return garbageRecordDetails.stream().map(g -> {
            GarbageRecordDetailDTO garbageRecordDetailDTO = new GarbageRecordDetailDTO();
            garbageRecordDetailDTO.setId(g.getId());
            garbageRecordDetailDTO.setGarbageRecordId(g.getGarbageRecordId());
            garbageRecordDetailDTO.setGarbageTypeId(g.getGarbageTypeId());
            garbageRecordDetailDTO.setAmount(g.getAmount());
            garbageRecordDetailDTO.setLocation(g.getLocation());
            garbageRecordDetailDTO.setUnit(g.getUnit());
            garbageRecordDetailDTO.setCreateBy(g.getCreateBy());
            garbageRecordDetailDTO.setCreateTime(g.getCreateTime());
            garbageRecordDetailDTO.setUpdateBy(g.getUpdateBy());
            garbageRecordDetailDTO.setUpdateTime(g.getUpdateTime());
            garbageRecordDetailDTO.setRemark(g.getRemark());
            GarbageType garbageType = garbageTypeMap.get(g.getGarbageTypeId());
            if (garbageType != null) {
                garbageRecordDetailDTO.setGarbageTypeName(garbageType.getName());
            }
            return garbageRecordDetailDTO;
        }).collect(Collectors.toList());
    }

    private GarbageRecordDetailDTO fillOneToDTO(GarbageRecordDetail garbageRecordDetail) {
        if (garbageRecordDetail == null) {
            return null;
        }
        GarbageRecordDetailDTO garbageRecordDetailDTO = new GarbageRecordDetailDTO();
        garbageRecordDetailDTO.setId(garbageRecordDetail.getId());
        garbageRecordDetailDTO.setGarbageRecordId(garbageRecordDetail.getGarbageRecordId());
        garbageRecordDetailDTO.setGarbageTypeId(garbageRecordDetail.getGarbageTypeId());
        garbageRecordDetailDTO.setAmount(garbageRecordDetail.getAmount());
        garbageRecordDetailDTO.setLocation(garbageRecordDetail.getLocation());
        garbageRecordDetailDTO.setUnit(garbageRecordDetail.getUnit());
        garbageRecordDetailDTO.setCreateBy(garbageRecordDetail.getCreateBy());
        garbageRecordDetailDTO.setCreateTime(garbageRecordDetail.getCreateTime());
        garbageRecordDetailDTO.setUpdateBy(garbageRecordDetail.getUpdateBy());
        garbageRecordDetailDTO.setUpdateTime(garbageRecordDetail.getUpdateTime());
        garbageRecordDetailDTO.setRemark(garbageRecordDetail.getRemark());
        GarbageType garbageType = garbageTypeService.selectById(garbageRecordDetail.getId());
        if (garbageType != null) {
            garbageRecordDetailDTO.setGarbageTypeName(garbageType.getName());
        }
        return garbageRecordDetailDTO;
    }
}
